#include "Lite_Queue.h"

#define  QUEUE_ITEM_NUM      3
#define  QUEUE_ITEM_SIZE     10

/*
* @ brief : Print the contents of each list item in the queue.
* @ param : {LiteQueue *} queue: Message queue handle pointer.
* @ return: None.
* @ author: bagy.
* @ note  : None.
*/
static void PrintLiteQueue(LiteQueue *queue)
{
    if(queue == NULL)
        return;

    for(int i = 0; i < queue->item_num; i++)
    {
        log_i("[item_num:%d] ", i);
        for(int n = 0; n < queue->item_size; n++)
        {
            log_i("%d ", *((uint8_t *)queue + sizeof(LiteQueue) + i * queue->item_size + n));
        }
        log_i("\n");
    }
}

/*
* @ brief : Print the data in buff.
* @ param : {const char *} mode : Print mode.
            {LiteQueue  *} queue: Message queue handle pointer.
* @ return: None.
* @ author: bagy.
* @ note  : Used to observe buff data changes and test to verify the correctness of written or read data.
*/
static void PrintBuff(const char *mode, uint8_t *buff, size_t len)
{  
    if((buff == NULL) || (len < 1))
        return;

    if(strstr(mode, "write"))
        log_i("Write buff>>>:");
    else
        log_i("Read buff <<<:");
    for(size_t i = 0; i < len; i++)
    {
        log_i("%d ", buff[i]);
    }

    log_i("\n\n");
}

/*
* @ brief : Queue callback function.
* @ param : {LiteQueue_Status} sta: Queue status.
* @ return: None.
* @ author: bagy.
* @ note  : When the queue writes or reads abnormally, the upper application will be notified through the callback function.
*/
void LiteQueue_Callback(LiteQueue_Status sta)
{
    switch(sta)
    {
        case LITE_QUEUE_FULL      : /*...*/ break;
        case LITE_QUEUE_WRITE_BUSY: /*...*/ break;
        case LITE_QUEUE_READ_BUSY : /*...*/ break;
        default:break;
    }
}

int main(void)
{
    uint8_t writebuff[QUEUE_ITEM_SIZE] = {0};
    uint8_t readbuff[QUEUE_ITEM_SIZE]  = {0};

    /* Create message queue, 4 list items, each list item has 10 bytes of memory space */
    pLiteQueue msgQueue = LiteQueue_Create(QUEUE_ITEM_NUM, QUEUE_ITEM_SIZE, LiteQueue_Callback);
    PrintLiteQueue(msgQueue);
    log_i("\n");

    /* Simulate writing and reading to the queue 6 times, and observe the data in the queue by printing */
    for(int i = 0; i < 4; i++)
    {
        /* Simulate data, change the writebuff data and write it to the queue */
        for(int n = 0; n < msgQueue->item_size; n++)
            writebuff[n] = (i * msgQueue->item_size + n) % 256;

        /* Data is written to the queue */
        Write_To_Back_LiteQueue(msgQueue, writebuff);
        PrintLiteQueue(msgQueue);

        log_i("\n");
    }

    /* Read data from queue */
    Read_From_LiteQueue(msgQueue, readbuff);
    PrintBuff("read", readbuff, sizeof(readbuff));

    /* Read data from queue */
    Read_From_LiteQueue(msgQueue, readbuff);
    PrintBuff("read", readbuff, sizeof(readbuff));

    PrintBuff("write", writebuff, sizeof(writebuff));
    /* Data is written to the queue */
    Write_To_Back_LiteQueue(msgQueue, writebuff);
    PrintLiteQueue(msgQueue);

    return 0;
}

